package elitech.vietnam.weatherfashion.notification;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import elitech.vietnam.weatherfashion.MainActivity;
import elitech.vietnam.weatherfashion.R;
import elitech.vietnam.weatherfashion.WFApplication;
import elitech.vietnam.weatherfashion.config.Const;
import elitech.vietnam.weatherfashion.config.GlobalValue;
import elitech.vietnam.weatherfashion.dto.Notify;
import elitech.vietnam.weatherfashion.notification.GCMIntentService.GCMEntity;
import elitech.vietnam.weatherfashion.service.PHPSoapServiceCaller;
import elitech.vietnam.weatherfashion.tabs.self.SelfPostFragment;
import elitech.vietnam.weatherfashion.tabs.styler.StylerDetailFragment;
import elitech.vietnam.weatherfashion.tabs.styler.StylerPostFragment;
import elitech.vietnam.weatherfashion.ultilities.TimeDiff;

public class NotificationArrayAdapter extends ArrayAdapter<Notify> implements OnItemClickListener {
	
	LayoutParams mLParam;
	MainActivity mActivity;
	
	public NotificationArrayAdapter(Context context, int resource, int textViewResourceId, List<Notify> objects) {
		super(context, resource, textViewResourceId, objects);
		mLParam = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		if (context instanceof MainActivity)
			mActivity = (MainActivity) context;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ViewHolder holder;
		
		if (row == null) {
			LayoutInflater inflater = LayoutInflater.from(getContext());
			row = inflater.inflate(R.layout.notification_list_item, parent, false);
			
			holder = new ViewHolder();
			holder.image = (ImageView) row.findViewById(R.id.notiitem_image);
			holder.title = (TextView) row.findViewById(R.id.notiitem_title);
			holder.content = (TextView) row.findViewById(R.id.notiitem_content);
			holder.time = (TextView) row.findViewById(R.id.notiitem_time);

			row.setTag(holder);
		} else
			holder = (ViewHolder) row.getTag();
		
		Notify noti = getItem(position);
		GCMEntity e = GCMEntity.newInstance(getContext(), noti);
		holder.title.setText(Html.fromHtml(e.bigtext));
		UrlImageViewHelper.setUrlDrawable(holder.image, Const.SERVER_IMAGE_URL + noti.Image, R.drawable.avatar);
		holder.content.setText(noti.Content);
		holder.time.setText(TimeDiff.getTimeDiffString(noti.Date(), getContext(), GlobalValue.getInstance(getContext()).readOptionInfo().getLanguageCode()));
		if (noti.Unread())
			row.setBackgroundResource(R.drawable.item_notification_unread);
		else
			row.setBackgroundResource(R.drawable.item_notification);
		
		row.setLayoutParams(mLParam);
		return row;
	}
	
	static class ViewHolder {
		ImageView image;
		TextView title, content, time;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		mActivity.mNotiWindow.dismiss();

		Notify noti = getItem(arg2);
		if (noti.Unread()) {
			for (Notify n : WFApplication.getInstance().mListNoti) {
				if (n.Id == noti.Id) {
					((WFApplication) getContext().getApplicationContext()).mBadge.increaseBadge(-1);
					Intent intent = new Intent();
					intent.setAction(Const.ACTION_INCREASE_BADGE);
					mActivity.sendBroadcast(intent);
					WFApplication.getInstance().mListNoti.remove(n);
					break;
				}
			}
		}
		
//		noti.isNew = false;
		noti.Unread = 0;

		new SetNotificationUnread().execute(noti.Id);
		
//		Bundle arg = new Bundle();
		switch (noti.Type) {
		case 1:
//			arg.putInt("POSTID", noti.PostID);
//			SelfPostFragment self = new SelfPostFragment();
//			self.setArguments(arg);
//			mActivity.getCurrentBase().replaceFragment(self, true);
			mActivity.getCurrentBase().replaceFragment(SelfPostFragment.init(noti.PostID), true);
			break;
		case 2:
//			arg.putInt("postID", noti.PostID);
//			arg.putString("memUID", noti.OwnerUID);
//			StylerPostFragment styler = new StylerPostFragment();
//			styler.setArguments(arg);
//			mActivity.getCurrentBase().replaceFragment(styler, true);
			mActivity.getCurrentBase().replaceFragment(StylerPostFragment.init(noti.PostID), true);
			break;
		case 3:
//			arg.putInt("memID", noti.SenderID);
//			StylerDetailFragment styler3 = new StylerDetailFragment();
//			styler3.setArguments(arg);
//			mActivity.getCurrentBase().replaceFragment(styler3, true);
			mActivity.getCurrentBase().replaceFragment(StylerPostFragment.init(noti.PostID), true);
			break;
		case 4:
//			arg.putInt("memID", noti.SenderID);
//			FriendListChiaLam2Fragment styler4 = new FriendListChiaLam2Fragment();
//			styler4.setArguments(arg);
//			mActivity.getCurrentBase().replaceFragment(styler4, true);
			mActivity.getCurrentBase().replaceFragment(StylerDetailFragment.init(noti.SenderID), true);
			break;
		default:
			break;
		}
	}
	
	static class SetNotificationUnread extends AsyncTask<Integer, Integer, Boolean> {
		@Override
		protected Boolean doInBackground(Integer... params) {
			PHPSoapServiceCaller service = new PHPSoapServiceCaller();
			try {
				service.readNotification(params[0]);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
	}
}
